package com.demo.config.express;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class SFLogisticsResponse {

	/**
	 * 校验返回结果
	 * @return ExpressSearchResponse
	 */
	public SFLogisticsSearchResponse checkResult() {
		SFLogisticsSearchResponse SFLogisticsSearchResponse = new SFLogisticsSearchResponse();
		return SFLogisticsSearchResponse;
	}

	/**
	 * 组装返回实体类
	 * @param SFLogisticsSearchResponse 返回信息
	 * @param jsonArr json数组
	 * @param request 入参
	 * @param descKey 描述
	 * @param dateKey 时间
	 * @return 组装返回实体类
	 */
	public SFLogisticsSearchResponse controller(SFLogisticsSearchResponse SFLogisticsSearchResponse, JSONArray jsonArr,
			ExpressQo request, String descKey, String dateKey) {
		if (jsonArr == null || jsonArr.isEmpty()) {
			return SFLogisticsSearchResponse;
		}
		List<ExpressVo> list = new ArrayList<>();
		for (Object j : jsonArr) {
			JSONObject json = JSONObject.parseObject(j.toString());
			ExpressVo expressVo = new ExpressVo();
			if (json.getString(descKey) == null || json.getString(dateKey) == null) {
				return SFLogisticsSearchResponse;
			}
			expressVo.setDesc(json.getString(descKey));
			expressVo.setDate(json.getString(dateKey));
			// 状态 0：邮寄中 1：已签收
			if (json.getString("opCode").equals("80") || json.getString("opCode").equals("8000")) {
				expressVo.setStatus("1");
			}
			else {
				expressVo.setStatus("0");
			}
			list.add(expressVo);
		}
		list = list.stream().sorted(Comparator.comparing(ExpressVo::getDate).reversed()).collect(Collectors.toList());
		SFLogisticsSearchResponse.setDescList(list);
		SFLogisticsSearchResponse.setLogisticsNo(request.getTrackingNumber().get(0));
		return SFLogisticsSearchResponse;
	}

}